package com.lzy.mall.seckill.controller;

import com.lzy.mall.common.utils.ResultUtils;
import com.lzy.mall.seckill.service.SeckillService;
import com.lzy.mall.seckill.vo.SeckillSkuRedisTo;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author lzy
 * @date 2021/10/15 11:50
 */
@RestController
@RequiredArgsConstructor(onConstructor_ = {@Autowired})
@Slf4j
public class SeckilController {

    private final SeckillService seckillService;

    /**
     * 返回当前时间可以参与秒杀的商品
     * @return
     */
    @ResponseBody
    @GetMapping("/currentSeckillSkus")
    public ResultUtils getCurrentSeckillSkus(){
        log.info("currentSeckillSkus正在执行......");
        List<SeckillSkuRedisTo> vos = seckillService.getCurrentSeckillSkus();
        return ResultUtils.ok().setData(vos);
    }

    /**
     * 查询当前订单是否正在秒杀
     * @param skuId
     * @return
     */
    @GetMapping("/sku/seckill/{skuId}")
    public ResultUtils getSkuSeckillInfo(@PathVariable("skuId") Long skuId){
        SeckillSkuRedisTo to = seckillService.getSkuSeckillInfo(skuId);
        return ResultUtils.ok().setData(to);
    }

    //TODO 上架秒杀商品得时候，每一个数据都有过期时间。
    //TODO 秒杀后续的流程，简化了收货地址等信息
    @GetMapping("/kill")
    public String seckill(@RequestParam("killId") String killId,
                          @RequestParam("key") String key,
                          @RequestParam("num") Integer num,
                          Model model){
        //1、判断是否登录 拦截器
        String orderSn = seckillService.kill(killId,key,num);
        model.addAttribute("orderSn",orderSn);
        return "success";
    }
}
